#include "thread.h"

int mysqlls(int netfd,sharInfo_t *sharInfo,MYSQL * mysql){

    char sql[1024];
    bzero(&sql,sizeof(sql));

    int ret;
    int parID=sharInfo->parentID;
    if(parID==-1){
        parID=sharInfo->rootID;
    }

    sprintf(sql,"select filename from df_mes where parID='%d'and tomb = 0;",parID);

    printf("sql = %s\n",sql);
    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"mysql_query2:%s\n",mysql_error(mysql));
        ret=-1;
        send(netfd,&ret,sizeof(ret),MSG_NOSIGNAL);
        return -1;
    }
    MYSQL_RES * result=mysql_store_result(mysql);
    MYSQL_ROW row;

    train_t train;
    bzero(&train,sizeof(train_t));

    printf("rows = %ld\n",mysql_num_rows(result));
    while((row=mysql_fetch_row(result))!=NULL){

            strcat( train.buf,row[0]);
            strcat( train.buf," " );
    }

    remove_trailing_invisible_chars(train.buf);
    printf("buf = %s\n",train.buf);
    train.length=strlen(train.buf);
    send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);
    send(netfd,&train.buf,train.length,MSG_NOSIGNAL);
    mysql_free_result(result);

    return 0;
}

